package com.rui.framework.context.support;

import com.rui.framework.beans.factory.support.BeanDefinitionReader;
import com.rui.framework.beans.factory.support.BeanDefinitionRegistry;
import com.rui.framework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : [锐神NO.3]
 * @version : [v1.0]
 * @className : AbstractApplicationContext
 * @createTime : [2022/2/26 16:29]
 * @description : [ApplicationContext接口的子实现类]
 */

public abstract class AbstractApplicationContext implements ApplicationContext {

    //声明解析器变量
    protected BeanDefinitionReader beanDefinitionReader;

    //定义用于存储bean的map容器
    protected Map<String, Object> singleObjects = new HashMap<>();

    //声明配置文件路径变量
    protected String configLocation;

    @Override
    public void refresh() throws Exception {
        //加载BeanDefinition对象
        beanDefinitionReader.loadBeanDefinitions(configLocation);
        finishBeanInitialization();
        //初始化bean
    }

    //bean的初始化
    public void finishBeanInitialization() throws Exception {
        //获取对应的注册表对象
        BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();

        //获取BeanDefinition对象
        String[] beanNames = registry.getBeanDefinitionNames();

        for (String beanName : beanNames) {
            //进行Bean的初始化
            getBean(beanName);
        }
    }
}
